package org.jeecg.modules.simulator.factory;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.orbit.OrbitPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-15 13:13
 **/
@Slf4j
public class TracksDbLoader {
    private static final Logger logger = LoggerFactory.getLogger(TracksDbLoader.class);
    @Setter
    List<Station> stationList = new ArrayList<>();


    @Getter
    List<OrbitPoint> orbitPath = new ArrayList<>();

    public void initTracksDb() {
//        System.out.println("初始化轨道数据");
        logger.debug("初始化轨道数据");
//        Map<String, List<Station>> groupByLineIdList = stationList.stream().collect(Collectors.groupingBy(Station::getLine));
        Map<String, List<Station>> groupByLineIdList = stationList.stream()
                .flatMap(station -> Arrays.stream(station.getLine().split(",")) // 按逗号分隔
                        .map(lineId -> new AbstractMap.SimpleEntry<>(lineId.trim(), station))) // 创建键值对
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                        Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); // 分组
        for (Map.Entry<String, List<Station>> entry : groupByLineIdList.entrySet()) {
            Collections.sort(entry.getValue(), (s1, s2) -> Long.compare(s1.getStartPt(), s2.getStartPt()));
            for (int i = 1; i < entry.getValue().size(); i++) {
                Station stationBefore = entry.getValue().get(i - 1);
                OrbitPoint  orbitPointStation= getFirstOrbitPoint( entry, i,stationBefore);
                orbitPath.add(orbitPointStation);
                OrbitPoint orbitPointSect = getSectOrbitPoint(entry, i, stationBefore);
                orbitPath.add(orbitPointSect);
            }

            OrbitPoint orbitPointStation1 = getSecondStationOrbitPoint(entry);
            orbitPath.add(orbitPointStation1);

        }



    }


    private OrbitPoint getFirstOrbitPoint(Map.Entry<String, List<Station>> entry,int i, Station stationBefore) {
        OrbitPoint orbitPointStation = new OrbitPoint();
        orbitPointStation.setOrbitName(stationBefore.getName());
        orbitPointStation.setStationId(stationBefore.getId());
        orbitPointStation.setIndex(2 * i - 1);
        orbitPointStation.setLineId(stationBefore.getLine());
        orbitPointStation.setStartOffsetDistance(stationBefore.getStartPt());
        orbitPointStation.setEndOffsetDistance(stationBefore.getEndPt());
        orbitPointStation.setBelongingType("station");
        return orbitPointStation;
    }

    private   OrbitPoint getSectOrbitPoint(Map.Entry<String, List<Station>> entry, int i, Station stationBefore) {
        OrbitPoint orbitPointSect = new OrbitPoint();
        Station stationA = entry.getValue().get(i);
        orbitPointSect.setOrbitName(stationBefore.getName() + "-" + stationA.getName());
        orbitPointSect.setStationId(stationBefore.getId()+"-"+stationA.getId());
        orbitPointSect.setBelongingType("section");

        orbitPointSect.setLineId(stationBefore.getLine());
        orbitPointSect.setIndex(2 * i);
        orbitPointSect.setStartOffsetDistance(stationBefore.getEndPt());
        orbitPointSect.setEndOffsetDistance(stationA.getStartPt());
        return orbitPointSect;
    }

    private  OrbitPoint getSecondStationOrbitPoint(Map.Entry<String, List<Station>> entry) {
        OrbitPoint orbitPointStation1 = new OrbitPoint();

        Station stationB = entry.getValue().get(entry.getValue().size() - 1);
        orbitPointStation1.setOrbitName(stationB.getName());
        orbitPointStation1.setIndex(2 * (entry.getValue().size()) - 1);
        orbitPointStation1.setLineId(stationB.getLine());
        orbitPointStation1.setStartOffsetDistance(stationB.getStartPt());
        orbitPointStation1.setEndOffsetDistance(stationB.getEndPt());
        orbitPointStation1.setBelongingType("station");
        return orbitPointStation1;
    }


}
